﻿using Dexter.Web.Mvc.Controllers;
using Dexter.Web.Mvc.Extensions;

namespace System.Web.Mvc {
	public static class ControllerExtensions {
		public static ActionResult ReturnFeedback ( this BackOfficeControllerBase controller, MessageType messageType, string resurceName ) {
			return ReturnFeedback ( controller , messageType , resurceName , null );
		}

		public static ActionResult ReturnFeedback ( this BackOfficeControllerBase controller, MessageType messageType, string resurceName, bool goToPreviousPage ) {
			return ReturnFeedback ( controller, messageType, resurceName, goToPreviousPage ? "back" : null );
		}

		public static ActionResult ReturnFeedback ( this DexterControllerBase controller, MessageType messageType, string resurceName, string redirectUrl ) {
			const string REDIRECTPAGE = "RedirectPage";

			if (!string.IsNullOrEmpty ( redirectUrl )) {
				if (controller.TempData[REDIRECTPAGE] == null)
					controller.TempData.Add ( REDIRECTPAGE, redirectUrl );
				else {
					controller.TempData[ REDIRECTPAGE ] = redirectUrl;
				}
			}
				
			else if(redirectUrl =="back") {
				if (controller.TempData[REDIRECTPAGE] == null)
					controller.TempData.Add ( REDIRECTPAGE, redirectUrl );
				else {
					controller.TempData[REDIRECTPAGE] = "javascript:history.back()";
				}
			}

			string msgType;

			switch ( messageType ) {
					case MessageType.Positive:
						msgType = "Positive";
						break;
					case MessageType.Negative:
						msgType = "Negative";
						break;
					case MessageType.Warning:
						msgType = "Warning";
						break;
					default:
						msgType = "Warning";
						break;
			}

			return controller.RedirectToAction ( msgType, "Message", new { Area = "Admin", id = resurceName } );
		}
	}
}
